package org.jianghu.activity.user.login.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.sns.SNS;
import com.avos.sns.SNSBase;
import com.avos.sns.SNSCallback;
import com.avos.sns.SNSException;
import com.avos.sns.SNSType;

import org.jianghu.R;
import org.jianghu.activity.user.OtherLoginView;
import org.jianghu.activity.user.OtherPresenterCompl;
import org.jianghu.activity.user.forgot.ForgotActivity;
import org.jianghu.activity.user.login.presenter.LoginPresenterCompl;
import org.jianghu.activity.user.regist.view.RegistActivity;
import org.jianghu.base.BaseActivity;
import org.jianghu.base.model.MsMessage;
import org.jianghu.utils.IntentUtil;
import org.jianghu.utils.ToastUtil;
import org.jianghu.view.CircleImageView;
import org.w3c.dom.Text;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2016/12/20.
 * 用户登录Activity
 */
@ContentView(R.layout.activity_user)
public class LoginActivity extends BaseActivity implements OtherLoginView,LoginView{

    OtherPresenterCompl otherPresenterCompl;//第三方登录
    LoginPresenterCompl loginPresenterCompl;//正常登陆逻辑

    @ViewInject(R.id.btn_close)
    private ImageView btn_close;

    @ViewInject(R.id.img_qq)
    private CircleImageView imgQQ;

    @ViewInject(R.id.btn_login)
    private Button btn_Login;

    @ViewInject(R.id.edt_user)
    private EditText edtUser;

    @ViewInject(R.id.edt_pwd)
    private EditText edtPsw;

    @ViewInject(R.id.txt_regist)
    private TextView txtRegist;

    @ViewInject(R.id.txt_forgot)
    private TextView txtForgot;

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if(!TextUtils.isEmpty(edtUser.getText().toString()) && !TextUtils.isEmpty(edtPsw.getText().toString())){
                btn_Login.setBackgroundColor(getResources().getColor(R.color.color_2095f2));
                btn_Login.setEnabled(true);
            }else{
                btn_Login.setBackgroundColor(getResources().getColor(R.color.hui_68));
                btn_Login.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    @Override
    public void initViewsAndValues(Bundle savedInstanceState) {
        x.view().inject(this);
        btnOk();
    }

    @Override
    public void initParmers() {
        otherPresenterCompl = new OtherPresenterCompl(this,LoginActivity.this);
        loginPresenterCompl = new LoginPresenterCompl(this);

    }

    @Override
    public void releaseOnDestory() {

    }

    @Event(value = {R.id.btn_close,R.id.img_qq,R.id.btn_login,R.id.txt_regist,R.id.txt_forgot})
    private void onclickListner(View view){
        switch (view.getId()){
            case R.id.btn_close:
                finish();
                break;
            case R.id.img_qq:
                otherPresenterCompl.qqLogin();
                break;

            case R.id.btn_login:

                loginPresenterCompl.doLogin(edtUser.getText().toString(),edtPsw.getText().toString());

                break;

            case R.id.txt_regist:
                IntentUtil.intentDIY(this, RegistActivity.class);
                break;

            case R.id.txt_forgot:
                IntentUtil.intentDIY(this, ForgotActivity.class);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SNS.onActivityResult(requestCode,resultCode,data,SNSType.AVOSCloudSNSQQ);
    }

    @Override
    public void resultQqLogin(boolean isQqLogin) {
        if(isQqLogin){
            ToastUtil.show(this,"QQ登陆成功", Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void resultSinaLogin(boolean isSinaLogin) {

    }

    @Override
    public void onLoginResult(boolean result,String message) {
        if(result){
            ToastUtil.show(this,"登陆成功",Toast.LENGTH_SHORT);
        }else {
            ToastUtil.show(this,message,Toast.LENGTH_SHORT);
        }
    }

    /*
        按钮是否可以点击
     */
    private void btnOk(){
        btn_Login.setBackgroundColor(getResources().getColor(R.color.hui_68));
        btn_Login.setEnabled(false);

        edtUser.addTextChangedListener(textWatcher);
        edtPsw.addTextChangedListener(textWatcher);
    }
}
